from ctypes import *

ret             = 0
AverDll         = None
iDevIndex       = -1
hWnd            = c_ulong(0)
hCaptureObj     = c_ulong(0)
hRecordObj      = c_ulong(0)

#load dll
def LoadAndInitDll():
    global AverDll
    AverDll = WinDLL("AVerCapAPI.dll")
    ret = AverDll.AVerInitialize()
    print ("AVerInitialize = ", ret)


#get device
def SelectDevice():
    global iDevIndex
    
    iDevNum = c_uint(0)
    pDevNum = pointer(iDevNum)
    ret = AverDll.AVerGetDeviceNum(pDevNum)
    print ("iDevNum = ", iDevNum )

    #list device name
    a = range(0, iDevNum.value)
    for i in a:
        pszDevName = create_unicode_buffer(256)
        AverDll.AVerGetDeviceName(c_uint(i), pszDevName)
        print (pszDevName.value)
    iDevIndex = int(input("Select Capture Device:"))
    if iDevIndex >= iDevNum.value:
        print ("Error no!")
        
        
#create capture device
def CreateCaptureObject():
    global hCaptureObj, hWnd
    dxExtStyle      = c_uint(0)
    lpClassName     = c_wchar_p("STATIC")
    lpWindowName    = c_wchar_p("AVerDemo")
    dwStyle         = c_uint(0)
    x               = c_int(0)
    y               = c_int(0)
    nWidth          = c_int(640)
    nHeight         = c_int(480)
    hWndParent      = c_ulong(0)
    hMenu           = c_ulong(0)
    hInstance       = windll.kernel32.GetModuleHandleW(None)
    lpParam         = c_ulong(0)

    hWnd = windll.user32.CreateWindowExW(dxExtStyle, lpClassName, lpWindowName,
                                         dwStyle, x, y, nWidth, nHeight,
                                         hWndParent, hMenu, hInstance, lpParam)

    windll.user32.ShowWindow(hWnd, c_uint(5))

 
    phCaptureObj = pointer(hCaptureObj)
    AverDll.AVerCreateCaptureObject(int(iDevIndex), hWnd, phCaptureObj)

#set video source
def SetVideoSource():  
    AverDll.AVerSetVideoSource(hCaptureObj, c_uint(3))

#set video resolution
def SetVideoResolution(): 
    class VIDEO_RESOLUTION(Structure):
        _fields_ = [("dwVersion", c_uint), ("dwVideoResolution", c_uint),
        ("bCustom", c_uint),("dwWidth", c_uint), ("dwHeight", c_uint)]                 
    Res = VIDEO_RESOLUTION(1, 7, 0, 0, 0)
    AverDll.AVerSetVideoResolutionEx(hCaptureObj,pointer(Res))

#run
def Run():     
    AverDll.AVerStartStreaming(hCaptureObj)



#function module
def FuncContorller():
    cmd = input("Input 'r' to record file or 'c' to capture image:")
    if cmd == "r":
       RecordFile()
    elif cmd == "c":
        CaptureImage()
        
#record file
def RecordFile():
    global hRecordObj

    '''
    class VIDEO_CAPTURE_INFO(Structure):
         _fields_ = [("dwCaptureType", c_uint), ("dwSaveType", c_uint), ("bOverlayMix", c_uint),
        ("dwDurationMode", c_uint), ("dwDuration", c_uint), ("lpFileName", c_wchar_p),
        ("VIDEOCAPTURECALLBACK", c_void_p), ("lCallbackUserData", c_void_p)]
            
        CaptureInfo = VIDEO_CAPTURE_INFO(1, 8, 0, 1, 0xffffffff, "c:\\debuglog\\aver_qt_test.mp4", None, None)
    '''
       
    phRecordObj = pointer(hRecordObj)
    AverDll.AVerStartRecordFile(hCaptureObj, phRecordObj, c_wchar_p("C:\Program Files (x86)\AVerMedia\AVM SDK\SDK Pro\Config\RecordConfig.ini"))

#capture image
def CaptureImage():
    class CAPTURE_IMAGE_INFO(Structure):
         _fields_ = [("dwVersion", c_uint), ("dwImageType", c_uint), ("dwCaptureType", c_uint),
        ("bOverlayMix", c_uint), ("dwDurationMode", c_uint), ("dwDuration", c_uint),
        ("dwCapNumPerSec", c_uint), ("rcCapRect.left", c_int), ("rcCapRect.top", c_int),
        ("rcCapRect.right", c_int), ("rcCapRect.bottom", c_int), ("lpFileName", c_wchar_p)]
            
    CaptureInfo = CAPTURE_IMAGE_INFO(1, 1, 1,   \
                                     1, 2, 10,  \
                                     0, 0, 0,   \
                                     0, 0, "c:\\debuglog\\AVerCapSDK.jpg")
    
       
    AverDll.AVerCaptureImageStart(hCaptureObj, pointer(CaptureInfo))    



#quit
def Stop():  
    input("Press any key to quit")
    if hRecordObj != 0:
        AverDll.AVerStopRecordFile(hRecordObj)
    if hCaptureObj != 0:
        AverDll.AVerStopStreaming(hCaptureObj)
    if hWnd != 0:
        windll.user32.DestroyWindow(hWnd)

if __name__ == '__main__':
    LoadAndInitDll()
    SelectDevice()
    if iDevIndex >=0:
        CreateCaptureObject()
        SetVideoSource()
        SetVideoResolution()
        Run()
        FuncContorller()
        Stop()
    
